<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Nguoidung_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
	}
		
	/**
	 * Get all Categories
	 *
	 * @access	public
	 * @return 	object	Categories
	 */
	function get_all_nguoidung($limit, $start)
	{
		$this->db->select('user.*,role.role_id as roleid, role.role_name');
		$this->db->from('user','role');
		$this->db->join('role', 'role.role_id = user.role_id');
		$this->db->where('user.deleted',0);
		$this->db->order_by('user.id','desc');
		$this->db->limit($limit, $start);
		//$this->db->where('product.created_by', $userid);
		$query = $this->db->get();
		return $query->result_array();
	}	
	
	function get_permission_by_role_id($role_id)
	{
		$this->db->select('*');
		$this->db->from('role_permission');
		$this->db->join('permission', 'permission.permission_id = role_permission.permission_id', 'left outer');
		$this->db->where('role_permission.role_id',$role_id);
		$query = $this->db->get();
		$return = $query->result_array();
		return $return;
	}
	function get_modules()
	{
		$this->db->select('*');
		$this->db->from('permission');
		$query = $this->db->get();
		return $query->result_array();
	}
	function get_role()
	{
		$this->db->select('*');
		$this->db->from('role');
		$query = $this->db->get();
		return $query->result_array();
	}
	function get_the_role($role_id)
	{
		
	}
	function add_role($role_id, $permission_id)
	{
		$data = array(
			'role_id' 		=> $role_id,
			'permission_id'		=> $permission_id
		);
		
		$this->db->set($data);
		$this->db->insert('role_permission');
	}
	
	function edit_role()
	{
		
	}
	function delete_roles($role_id,$not_in_array)
	{
		$this->db->where('role_id', $role_id);
		$this->db->where_not_in('permission_id', $not_in_array);
		$this->db->delete('role_permission');
	}
	// --------------------------------------------------------------------
	
	/**
	 * Get all entries
	 *
	 * @access	public
	 * @return 	object	Entries
	 */
	function add_nguoidung()
	{
		//$ngaysinh = ($this->input->post('ngaysinh') != '') ? date('Y-m-d', strtotime($this->input->post('ngaysinh'))) : date('Y-m-d');
		$date = str_replace('/', '-', $this->input->post('ngaysinh'));
		$birthdate = date('Y-m-d', strtotime($date));
		$data = array(
			'username' 		=> $this->input->post('username'),
			'fullname'		=> $this->input->post('fullname'),
			'email'			=> $this->input->post('email'),
			'mobile'		=> $this->input->post('didong'),
			'address'		=> $this->input->post('address'),
			'role_id'		=> $this->input->post('role'),
			'birthdate'		=> $birthdate,
			'active'		=> $this->input->post('status'),
			'deleted'		=> 0,
		);
		
		$this->db->set($data);
		$this->db->insert('user');
	}
	
	function add_user()
	{
		$password = $this->input->post('password');
		$hash = sha1(microtime());
		$salt = $this->config->item('auth');
		$salt = $salt['salt'];

		$password = sha1($salt.$hash.$password);
		$data = array(
			'username' 			=> $this->input->post('username'),
			'password'			=> $password,
			'hash'				=> $hash,
			'email'				=> $this->input->post('email'),
			'fullname'			=> $this->input->post('fullname'),
			'mobile'			=> $this->input->post('didong'),
			'FK_group_id'		=> $this->input->post('group'),
			'active'			=> $this->input->post('status'),
		);
		
		$this->db->set($data);
		$this->db->insert('user');
	}
	
	public function check_username()
	{
		$username = $this->input->post('username');
        $query =  "
			SELECT id, username
			FROM user WHERE username ='$username' 
			";
        $result = $this->db->query($query);
        if($result->num_rows() >= 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	public function check_cmnd()
	{
        $this->db->where('cmnd',$this->input->post('cmnd'));
        $query = $this->db->get('hocvien');
        if($query->num_rows() == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	public function check_malop()
	{
        $this->db->where('malop',$this->input->post('malop'));
        $query = $this->db->get('lophoc');
        if($query->num_rows() == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	
    
	public function check_email()
	{
        $this->db->where('email',$this->input->post('email'));
        $query = $this->db->get('user');
        if($query->num_rows() == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	public function get_nguoidung_info($id)
	{
		$this->db->select('*');
		$this->db->from('user');
		$this->db->where('id', $id);
		$query = $this->db->get();
		return $query->row();
	}
	
	public function edit_nguoidung($id)
	{	
		//$ngaysinh = ($this->input->post('ngaysinh') != '') ? date('Y-m-d', strtotime($this->input->post('ngaysinh'))) : date('Y-m-d');
		$password = $this->input->post('password');
		$hash = sha1(microtime());
		$salt = $this->config->item('auth');
		$salt = $salt['salt'];
		
		$date = str_replace('/', '-', $this->input->post('ngaysinh'));
		$birthdate = date('Y-m-d', strtotime($date));
		$password = sha1($salt.$hash.$password);
		$fields = array(
			'fullname' 		=> $this->input->post('fullname'),
			'username'		=> $this->input->post('username'),
			'email'			=> $this->input->post('email'),
			'mobile'		=> $this->input->post('didong'),
			'address'		=> $this->input->post('address'),
			'role_id'		=> $this->input->post('role'),
			'active'		=> $this->input->post('status'),
			'birthdate'		=> $birthdate,		
		);
		
		$fields2 = array(
			'fullname' 		=> $this->input->post('fullname'),
			'username'		=> $this->input->post('username'),
			'email'			=> $this->input->post('email'),
			'password'			=> $password,
			'hash'				=> $hash,
			'mobile'		=> $this->input->post('didong'),
			'address'		=> $this->input->post('address'),
			'role_id'		=> $this->input->post('role'),
			'active'		=> $this->input->post('status'),
			'birthdate'		=> $birthdate,
			
		);
		
		
		if($this->input->post('password') == '')
		{
			$this->db->set($fields);
		}
		else
		{
			$this->db->set($fields2);
		}
		
		
		$this->db->where('id', $id);
		$this->db->update('user');
	}
	
	function delete_nguoidung($id)
	{
		$fields = array(
			'status' 			=> 0,
			'deleted' 			=> 1,
		);
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->update('user');
	}
	

	
	public function change_status_nguoidung($id = null,$status = 0)
	{
		$data = array
				(
					'active'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('user',$data);		
	}
	
	
	public function search_data($keyword)
	{
		$this->db->select('*');
		$this->db->like('username', $keyword);
		$this->db->or_like('fullname', $keyword);
		$this->db->order_by('id', 'DESC');
		$query = $this->db->get('user');
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}

	public function count_all_nguoidung()
	{
		$this->db->select('id');
		$this->db->from('user');
		return $this->db->count_all_results();
	}
}
// END listings_model.php
/* Location: ./application/models/listings_model.php */
